package com.bilin.coin.fragment.mill;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import com.bilin.coin.R;
import com.bilin.coin.bean.CommonSuccessEvent;
import com.bilin.coin.bean.MillResourceBean;
import com.bilin.coin.common.base.BaseAppCompatActivity;
import com.bilin.coin.common.base.BaseFragment;
import com.bilin.coin.list.MillResourceList;
import com.bilin.coin.list.MyMillList;
import com.bilin.coin.tools.Poster;
import com.chad.library.adapter.base.BaseQuickAdapter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;

public class MyMillFragment extends BaseFragment {



    @BindView(R.id.ll_main)
    LinearLayout ll_main;

    MyMillList digToolsList;

    public static MyMillFragment newInstance(int type) {

        Bundle args = new Bundle();
        MyMillFragment fragment = new MyMillFragment();
        args.putInt("type", type);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {

        Bundle arguments = getArguments();
        int type = arguments.getInt("type");

        digToolsList = new MyMillList((BaseAppCompatActivity) mContext,type);
        //digToolsList.refresh(true);
        ll_main.addView(digToolsList.getRootView(), new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

        digToolsList.getAdapter().setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                MillResourceBean millResourceBean = (MillResourceBean) adapter.getData().get(position);
                buy(millResourceBean.getId());
            }
        });
    }

    private void buy(String id) {
        new Poster((BaseAppCompatActivity) mContext, true, true) {
            @Override
            protected String fillUrl() {
                return "/api/bl-device/buy";
            }

            @Override
            protected Map<String, String> fillParams() {
                HashMap hashMap = new HashMap();
                hashMap.put("id", id);
                return hashMap;
            }

            @Override
            protected void disposeSuccess(String dataJson) {
                super.disposeSuccess(dataJson);
                digToolsList.refresh(true);
            }
        };
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.common_layout;
    }

    @Override
    public void onStart() {
        super.onStart();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    /**
     * 登录成功
     *
     * @param commonSuccessEvent
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void loginSuccessEvent(CommonSuccessEvent commonSuccessEvent) {
        if (commonSuccessEvent.getType() == CommonSuccessEvent.LoginSuccess) {
            digToolsList.refresh(true);
        }
        EventBus.getDefault().removeStickyEvent(commonSuccessEvent);
    }

    /**
     * 认证成功
     *
     * @param commonSuccessEvent
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void buyDevicesSuccessEvent(CommonSuccessEvent commonSuccessEvent) {
        if (commonSuccessEvent.getType() == CommonSuccessEvent.BuyDevicesSuccess) {

            digToolsList.refresh(true);
        }
        EventBus.getDefault().removeStickyEvent(commonSuccessEvent);
    }
}
